import Vue from 'vue'
import Router from 'vue-router'

import HomeView from './views/Home.vue'
import NewsView from './views/News.vue'
import NewsDetailView from './views/ContentDetail.vue'
import ActivityView from './views/Activity.vue'
import ActivityDetailView from './views/ContentDetail.vue'
import CourseView from './views/Course.vue'
import ArticlesView from './views/Articles.vue'
import ArticleDetailView from './views/ContentDetail.vue'
import CorporationView from './views/Corporation.vue'
import TipsView from './views/Tips.vue'
import AboutView from './views/About.vue'
import MallView from './views/Mall.vue'
import ProductDetailView from './views/ProductDetail.vue'

import RecruitmentView from './views/trivial/Recruitment.vue'
import DisclaimerView from './views/trivial/Disclaimer.vue'
import CopyrightView from './views/trivial/Copyright.vue'
import PrivacyView from './views/trivial/Privacy.vue'

Vue.use(Router)

const scrollBehavior = (to, from, savedPosition) => {
  if (savedPosition) {
    // savedPosition is only available for popstate navigations.
    return savedPosition
  } else {
    const position = {}
    // new navigation.
    // scroll to anchor by returning the selector
    if (to.hash) {
      position.selector = to.hash
    }
    // check if any matched route config has meta that requires scrolling to top
    if (to.matched.some(m => m.meta.scrollToTop)) {
      // cords will be used if no selector is provided,
      // or if the selector didn't match any element.
      position.x = 0
      position.y = 0
    }
    // if the returned position is falsy or an empty object,
    // will retain current scroll position.
    return position
  }
}

export default new Router({
  mode: 'history',
  scrollBehavior,
  routes: [
    { path: '/home', component: HomeView, meta: { scrollToTop: true } },
    { path: '/news', component: NewsView, meta: { scrollToTop: true } },
    { path: '/news/:newsId', name: 'news', component: NewsDetailView, meta: { scrollToTop: true } },
    { path: '/activities', component: ActivityView, meta: { scrollToTop: true }},
    { path: '/activities/:activityId', name: 'activity', component: ActivityDetailView, meta: { scrollToTop: true } },
    { path: '/courses', component: CourseView, meta: { scrollToTop: true } },
    { path: '/articles', component: ArticlesView, meta: { scrollToTop: true } },
    { path: '/articles/:articleId', name: 'article', component: ArticleDetailView, meta: { scrollToTop: true } },
    { path: '/corporations', component: CorporationView, meta: { scrollToTop: true } },
    { path: '/products', component: MallView, meta: { scrollToTop: true } },
    { path: '/products/:productId', name: 'product', component: ProductDetailView, meta: { scrollToTop: true } },
    { path: '/tips', component: TipsView, meta: { scrollToTop: true } },
    { path: '/about', component: AboutView, meta: { scrollToTop: true } },
    { path: '/recruitment', component: RecruitmentView, meta: { scrollToTop: true } },
    { path: '/disclaimer', component: DisclaimerView, meta: { scrollToTop: true } },
    { path: '/copyright', component: CopyrightView, meta: { scrollToTop: true } },
    { path: '/privacy', component: PrivacyView, meta: { scrollToTop: true } },

    // Redirect
    { path: '/', redirect: '/home' , meta: { scrollToTop: true } },
    { path: '*', redirect: '/home' }
  ]
})